package com.dzjcloud.dzj.upms.biz.controller.sys;


import cn.hutool.core.util.StrUtil;

import com.dzjcloud.dzj.common.core.constant.CommonConstants;
import com.dzjcloud.dzj.common.core.enums.ResultEnum;
import com.dzjcloud.dzj.common.core.utils.GetTime;
import com.dzjcloud.dzj.common.core.utils.R;
import com.dzjcloud.dzj.common.core.exception.HandleException;
import com.dzjcloud.dzj.upms.api.entity.Org;
import com.dzjcloud.dzj.upms.biz.service.OrgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 机构表(Org)表控制层
 *
 * @author makejava
 * @since 2021-06-17 11:00:23
 */
@RestController
@RequestMapping(CommonConstants.SYS+"org")
@Slf4j
@Validated
@RequiredArgsConstructor
@Api(tags = "机构前端控制器",value = "机构前端控制器")
public class SysOrgController {
    /**
     * 服务对象
     */
    private final OrgService orgService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("通过主键查询单条数据")
    @GetMapping("/{id}")
    public R<Org> selectOne(@ApiParam(value = "机构Id",required = true)@PathVariable String id) {
        return R.ok(this.orgService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param org 实体对象
     * @return 新增结果
     */
    @ApiOperation("新增数据")
    @PostMapping("/add")
    public R<Boolean> insert(@RequestBody Org org) {
        org.setId(GetTime.getGuid());
        if (StrUtil.isBlank(org.getOrgName())){
            throw new HandleException(ResultEnum.PARAM_IS_ERROR.getCode(),"机构名称不能为空");
        } else if(StrUtil.isBlank(org.getOrgResponsibleUserId())){
            throw new HandleException(ResultEnum.PARAM_IS_ERROR.getCode(),"机构负责人不能为空");
        }
        return R.ok(this.orgService.save(org));
    }

    /**
     * 修改数据
     *
     * @param org 实体对象
     * @return 修改结果
     */
    @ApiOperation("修改数据")
    @PutMapping("/update")
    public R<Boolean> update(@RequestBody Org org) {
        if (StrUtil.isBlank(org.getOrgName())){
            throw new HandleException(ResultEnum.PARAM_IS_ERROR.getCode(),"机构名称不能为空");
        } else if(StrUtil.isBlank(org.getOrgResponsibleUserId())){
            throw new HandleException(ResultEnum.PARAM_IS_ERROR.getCode(),"机构负责人不能为空");
        }
        return R.ok(this.orgService.updateById(org));
    }

    /**
     * 删除数据
     *
     * @return 删除结果
     */
    @DeleteMapping("/delete/{id}")
    public R<Boolean> delete(@ApiParam("机构Id")@PathVariable String id) {
        return R.ok(this.orgService.removeById(id));
    }
}
